package com.shop.controller;

import com.alibaba.fastjson.JSON;
import com.shop.entity.ShowPageT;
import com.shop.entity.User;
import com.shop.servlet.impl.UserServletImpl;
import com.shop.tools.ConstantPool;
import jakarta.servlet.annotation.WebServlet;
import org.mindrot.jbcrypt.BCrypt;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.UUID;

/**
 * com.angluzha.shop.controller文档说明
 *
 * @author Angluzha
 * @version 2022/9/13
 */
@WebServlet("/user/*")
public class UserController extends BaseController{
    {
        servlet = new UserServletImpl();
    }
    public String save(User user){
        servlet.insert(user);
        return redirect("/user/selectAll");
    }
    public String delete(User user){
        servlet.deleteById(user.getId());
        return redirect("/user/selectAll");
    }
    public String update(User user){
        servlet.update(user);
        return redirect("/user/selectAll");
    }
    public String selectAll(){
        ShowPageT<User> pageT = (ShowPageT<User>) req.getAttribute("pageT");
        page(pageT,"/user/selectAll");
        return forward("/back/userjsp/userinfo.jsp");
    }
    public String delCheck(){
        try {
            String line = req.getReader().readLine();
            List<Integer> userList = JSON.parseArray(line, Integer.class);
            if(servlet.delCheck(userList)){
                return writeWeb("200");
            }else {
                return writeWeb("100");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public String login(User user) throws IOException {
        System.out.println(user);
        String password = user.getPassword();
        UserServletImpl servlet = (UserServletImpl)this.servlet;
        Map<String,Object> map = servlet.login(user);
        if (map.get("code").equals(200)){
            req.getSession().setAttribute("user",map.get("user"));
            return redirect("/index.jsp");
        }
        return redirect("/login.jsp");
    }
    public String quit(){
        req.getSession().removeAttribute("user");
        return redirect("/login.jsp");
    }
    public String exist(User user){
       user = (User) servlet.selectOne(user);
       if (user!=null){
           return writeWeb("0");
       }else {
           return writeWeb("1");
       }
    }
    public String register(User user){
        System.out.println(user);
        try {
            if (exist(user).equals(ConstantPool.JSON+ "~"+"0")) {
                return writeWeb("0");//0 账号已存在 //1 账号待激活 //2出现错误
            }else{
                //http://localhost:8080/user/addUser?email=1078006503@qq.com&username=stringto&password=654321
                UserServletImpl servlet = (UserServletImpl)this.servlet;
                return  servlet.verification(user)?writeWeb("1"):writeWeb("2");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public String registerActiveUser(){
        String code = req.getParameter("code");
        UserServletImpl servlet = (UserServletImpl)this.servlet;
        return (servlet.activation(code))?redirect("http://localhost:8080/registerSuccess.html"):redirect("http://localhost:8080/registerFalse.html");
    }
}
